package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.util.SortedList;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import www.openteam.com.R;
import www.openteam.com.bean.ProStock;
import www.openteam.com.bean.ProducePro;

/**
 * Created by kylin on 2016/8/19.
 * 库存查询adapter
 */
public class StockAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 产品进度适配器
     */
    private static final String TAG = "ProAdapter";
    private LayoutInflater mLayoutInflater;
    private SortedList<ProStock.DataEntity> mSortedList;

    public StockAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(ProStock.DataEntity.class, new SortedList.Callback<ProStock.DataEntity>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(ProStock.DataEntity o1, ProStock.DataEntity o2) {

                if (Integer.parseInt(o1.getId()) < Integer.parseInt(o2.getId())) {
                    return -1;
                } else if (Integer.parseInt(o1.getId()) > Integer.parseInt(o2.getId())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(ProStock.DataEntity oldItem, ProStock.DataEntity newItem) {
                return oldItem.getId().equals(oldItem.getId());
            }

            @Override
            public boolean areItemsTheSame(ProStock.DataEntity item1, ProStock.DataEntity item2) {
                return item1.getId() == item2.getId();
            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<ProStock.DataEntity> list) {
        mSortedList.beginBatchedUpdates();
        for (ProStock.DataEntity itemModel : list) {
            mSortedList.add(itemModel);
        }
        mSortedList.endBatchedUpdates();
    }

    public void deleteItems(ArrayList<ProStock.DataEntity> items) {
        mSortedList.beginBatchedUpdates();
        for (ProStock.DataEntity item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new StockAdapter.stViewHolder(mLayoutInflater.inflate(R.layout.item_base_no_audit, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        Log.d(TAG, "DataAdapter:4");
        ProStock.DataEntity data = mSortedList.get(position);

        StockAdapter.stViewHolder viewHolder = (StockAdapter.stViewHolder) holder;
        viewHolder.textView.setText("编号:"+data.getId()+"");

        viewHolder.tv_title.setText("货品名称:"+data.getName() + "\n" +"规格:"+data.getSpecs()+
                "\n" + "克重:"+data.getGWeight() + "\n"+"品牌:" + data.getBrandNo()+
                "\n" + "库存数量:"+data.getNumb() + "\n"+"单位:" + data.getUnit());
    }


    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class stViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private TextView tv_title;

        public stViewHolder(View itemView) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
            textView = (TextView) itemView.findViewById(R.id.tv_id);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);

        }
    }




}